package medium;

import pojo.TreeNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

public class T2415 {
    public T2415() {

    }

    // 树的层级遍历
    public void treeLevel(TreeNode root) {
        List<TreeNode> rootQueue = new ArrayList<>();
        rootQueue.add(root);
        int index = 1;
        int level = 2;
        while (true) {
            List<TreeNode> nodeQueue = new ArrayList<>();
            if (rootQueue.get(0).left== null){
                break;
            }
            for (TreeNode node : rootQueue) {
                nodeQueue.add(node.left);
                nodeQueue.add(node.right);
            }
            if (level % 2 == 0) {
                int i = 0;
                int j = nodeQueue.size() - 1;
                while (i < j) {
                    int val = nodeQueue.get(i).val;
                    nodeQueue.get(i).val = nodeQueue.get(j).val;
                    nodeQueue.get(j).val = val;
                    i++;
                    j--;
                }
            }
            level++;
            rootQueue.clear();
            rootQueue.addAll(nodeQueue);
        }
    }
}
